package org.xtframe.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.xtframe.common.ActiveUserListener;
import org.xtframe.common.json.JSON2Java;
import org.xtframe.entity.ServerData;
import org.xtframe.entity.UserData;
import org.xtframe.service.AjaxDataHandler;
import org.xtframe.service.AjaxDataHandlerException;
import org.xtframe.util.CommonUtil;
import org.xtframe.util.ContextUtil;
import org.xtframe.util.StringUtil;

@Service
@Scope("prototype")
public class ServerDataDataHandler implements AjaxDataHandler {

	private final Log logger = LogFactory.getLog(getClass());

	public Map<String, Object> execute(String strData) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			UserData userData = ContextUtil.getCurrentUserData();
			ServerData serverData = ContextUtil.getServerData();
			
			Map<String, Object> objJson = JSON2Java.optMap(CommonUtil.getJavaObject(strData));
			String name = StringUtil.toString(objJson.get("name"), "");
			if(StringUtil.isNullStr(name)) throw new AjaxDataHandlerException("获取服务器数据的名称为空");
			
			name = name.toLowerCase();
			if(name.equals("uuid")){
				map.put(STATUS, true);
				map.put(OBJECT, CommonUtil.getUUID());
			} else if(name.equals("optrole")){
				boolean flag = false;
				ArrayList<String> roleList = userData.getRoleList();
				for(String roleid : roleList){
					if(roleid.equals("admin")){
						flag = true;
						break;
					}
				}
				
				if(!(flag && (userData.getUser().getOrganId() == 1) && (userData.getUser().getJobId() == 1))){
					ArrayList<Map<String, String>> optRoleList = userData.getOptRoleList();
					if(optRoleList != null){
						map.put(STATUS, true);
						map.put(OBJECT, optRoleList);
					} else {
						map.put(STATUS, false);
					}
				} else{
					ArrayList<Map<String, String>> optRoleList = serverData.getOptRole();
					if(optRoleList != null){
						map.put(STATUS, true);
						map.put(OBJECT, optRoleList);
					} else {
						map.put(STATUS, false);
					}
				}
			} else if(name.equals("regid")){
				String regName = StringUtil.toString(objJson.get("parameters"), "");
				if(StringUtil.isNullStr(regName)) throw new AjaxDataHandlerException("获取服务器数据序列号的名称为空");
				
				map.put(STATUS, true);
				map.put(OBJECT, CommonUtil.getRegID(regName));
			} else if(name.equals("logic")){
				String logicId = StringUtil.toString(objJson.get("parameters"), "");
				if(StringUtil.isNullStr(logicId)) throw new AjaxDataHandlerException("获取服务器数据判断逻辑的名称为空");
				
				map.put(STATUS, true);
				map.put(OBJECT, userData.isInLogic(logicId));
			} else if(name.equals("curr_date")){
				map.put(STATUS, true);
				map.put(OBJECT, (new Date()).toString());
			} else if(name.equals("curr_user_atorganid")){
				map.put(STATUS, true);
				map.put(OBJECT, userData.getOrgan().getAtOrganId());
			} else if(name.equals("curr_user_organid")){
				map.put(STATUS, true);
				map.put(OBJECT, userData.getOrgan().getOrganId());
			} else if(name.equals("dictionary")){
				String dicId = StringUtil.toString(objJson.get("parameters"), "");
				if(StringUtil.isNullStr(dicId)) throw new AjaxDataHandlerException("获取服务器数据字典的名称为空");
				
				map.put(STATUS, true);
				map.put(OBJECT, serverData.getDictionaryDataList(dicId));
			} else if(name.equals("organdata")){
				String orgDataName = StringUtil.toString(objJson.get("parameters"), "");
				if(StringUtil.isNullStr(orgDataName)) throw new AjaxDataHandlerException("获取服务器数据机构数据的名称为空");
				
				map.put(STATUS, true);
				map.put(OBJECT, userData.getOrganDataValue(orgDataName));
			} else if(name.equals("systemdata")){
				String sysDataName = StringUtil.toString(objJson.get("parameters"), "");
				if(StringUtil.isNullStr(sysDataName)) throw new AjaxDataHandlerException("获取服务器数据系统数据的名称为空");
				
				map.put(STATUS, true);
				map.put(OBJECT, serverData.getSystemDataValue(sysDataName));
			} else if(name.equals("useronlinecount")){
				map.put(STATUS, true);
				map.put(OBJECT, ActiveUserListener.getUserOnlineCount());
			} else if(name.equals("useronlinecode")){
				map.put(STATUS, true);
				map.put(OBJECT, ActiveUserListener.getUserOnlineCode());
			} else {
				map.put(STATUS, false);
			}
		} catch(AjaxDataHandlerException ex){
			logger.warn(ex.toString());
			map.clear();
			map.put(STATUS, false);
		} catch (Exception e) {
			logger.error(e.toString());
			map.clear();
			map.put(STATUS, false);
		}
		
		return map;
	}

}
